package arbor.mining.util.stock;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

import arbor.mining.util.date.DateUtil;
import arbor.mining.util.net.WebStringConn;

public class CNYesGather extends StockDataGather{

	public CNYesGather() {
		super();
	}

	public StockEntity[] getInstantWTXPrice() {
		StockEntity wtx1Entity = null;
		StockEntity wtx0Entity = null;
		WebStringConn conn = new WebStringConn(null);
		logger.info("Get " + urlProp.getProperty("TODAY_INSTANT_INDEX_URL"));
		String dayc = conn.getAllTableContent((urlProp
				.getProperty("TODAY_INSTANT_INDEX_URL")), false,false);
		//System.out.println(dayc);
		StockEntity[] wtx = parseInstantPrice(dayc);
		return wtx;
	}

	private StockEntity[] parseInstantPrice(String dayc) {

		DateFormat hdf = new SimpleDateFormat("hh:mm");
		StockEntity[] entities = new StockEntity[3];
        boolean wtx0Found = false;
        boolean wtx1Found = false;
        boolean wtx2Found = false;
        
		StringTokenizer token = new StringTokenizer(dayc, "\n");
		String line = token.nextToken();
        String prevLine = null;
		while (wtx0Found == false) {
			if (!line.startsWith("台灣加權")) {
				prevLine = line;
				line = token.nextToken();
			} else {
				
				String time = prevLine;
				Date min;
				try {
					min = hdf.parse(time);
				} catch (ParseException e) {
					logger.warn("No time info");
					min = null;
				}
				if (min != null) {
					GregorianCalendar startTime = new GregorianCalendar();
					GregorianCalendar endTime = new GregorianCalendar();

					startTime.setTime(min);
					startTime.set(GregorianCalendar.getInstance().get(
							Calendar.YEAR), GregorianCalendar.getInstance()
							.get(Calendar.MONTH), GregorianCalendar
							.getInstance().get(Calendar.DATE));
					endTime.setTime(min);
					endTime.set(GregorianCalendar.getInstance().get(
							Calendar.YEAR), GregorianCalendar.getInstance()
							.get(Calendar.MONTH), GregorianCalendar
							.getInstance().get(Calendar.DATE));

					endTime.set(Calendar.HOUR_OF_DAY, 13);
					endTime.add(Calendar.MINUTE, 1);
					entities[0] = new StockEntity(
							StockEntity.RANGE_TYPE_DAY, startTime, endTime);
				} else {
					Date todayday = DateUtil.getCurrentDate();
					GregorianCalendar startTime = new GregorianCalendar();
					GregorianCalendar endTime = new GregorianCalendar();

					startTime.setTime(todayday);
					startTime.set(Calendar.HOUR_OF_DAY, 9);
					startTime.set(Calendar.MINUTE, 0);
					startTime.set(Calendar.SECOND, 0);
					endTime.setTime(todayday);
					endTime.set(Calendar.HOUR_OF_DAY, 13);
					endTime.set(Calendar.MINUTE, 30);
					endTime.set(Calendar.SECOND, 0);
					entities[0] = new StockEntity(
							StockEntity.RANGE_TYPE_DAY, startTime, endTime);
				}

				/* 成交價 */
				token.nextToken();
				token.nextToken();
				entities[0].ohlc.close = Double.parseDouble(token
						.nextToken());
				token.nextToken();
				token.nextToken();
				/* 開盤價 最高 最低 */
				entities[0].ohlc.open = Double.parseDouble(token
						.nextToken());
				entities[0].ohlc.high = Double.parseDouble(token
						.nextToken());
				entities[0].ohlc.low = Double.parseDouble(token.nextToken());

				wtx0Found = true;
			}
		}

		line = token.nextToken();
		while (wtx1Found == false) {
			if (!line.startsWith("台指期近1")) {
				prevLine = line;
				line = token.nextToken();
			} else {
				
				String time = prevLine;
				Date min;
				try {
					min = hdf.parse(time);
				} catch (ParseException e) {
					logger.warn("No time info");
					min = null;
				}
				if (min != null) {
					GregorianCalendar startTime = new GregorianCalendar();
					GregorianCalendar endTime = new GregorianCalendar();

					startTime.setTime(min);
					startTime.set(GregorianCalendar.getInstance().get(
							Calendar.YEAR), GregorianCalendar.getInstance()
							.get(Calendar.MONTH), GregorianCalendar
							.getInstance().get(Calendar.DATE));
					endTime.setTime(min);
					endTime.set(GregorianCalendar.getInstance().get(
							Calendar.YEAR), GregorianCalendar.getInstance()
							.get(Calendar.MONTH), GregorianCalendar
							.getInstance().get(Calendar.DATE));

					endTime.set(Calendar.HOUR_OF_DAY, 13);
					endTime.add(Calendar.MINUTE, 1);
					entities[1] = new StockEntity(
							StockEntity.RANGE_TYPE_DAY, startTime, endTime);
				} else {
					Date todayday = DateUtil.getCurrentDate();
					GregorianCalendar startTime = new GregorianCalendar();
					GregorianCalendar endTime = new GregorianCalendar();

					startTime.setTime(todayday);
					startTime.set(Calendar.HOUR_OF_DAY, 9);
					startTime.set(Calendar.MINUTE, 0);
					startTime.set(Calendar.SECOND, 0);
					endTime.setTime(todayday);
					endTime.set(Calendar.HOUR_OF_DAY, 13);
					endTime.set(Calendar.MINUTE, 30);
					endTime.set(Calendar.SECOND, 0);
					entities[1] = new StockEntity(
							StockEntity.RANGE_TYPE_DAY, startTime, endTime);
				}

				/* 成交價 */
				token.nextToken();
				token.nextToken();
				entities[1].ohlc.close = Double.parseDouble(token
						.nextToken());
				token.nextToken();
				token.nextToken();
				/* 開盤價 最高 最低 */
				entities[1].ohlc.open = Double.parseDouble(token
						.nextToken());
				entities[1].ohlc.high = Double.parseDouble(token
						.nextToken());
				entities[1].ohlc.low = Double.parseDouble(token.nextToken());
				entities[1].volume = Integer.parseInt(token.nextToken());
				wtx1Found = true;
			}
		}		

		line = token.nextToken();
		while (wtx2Found == false) {
			if (!line.startsWith("台指期近2")) {
				prevLine = line;
				line = token.nextToken();
			} else {
				
				String time = prevLine;
				Date min;
				try {
					min = hdf.parse(time);
				} catch (ParseException e) {
					logger.warn("No time info");
					min = null;
				}
				if (min != null) {
					GregorianCalendar startTime = new GregorianCalendar();
					GregorianCalendar endTime = new GregorianCalendar();

					startTime.setTime(min);
					startTime.set(GregorianCalendar.getInstance().get(
							Calendar.YEAR), GregorianCalendar.getInstance()
							.get(Calendar.MONTH), GregorianCalendar
							.getInstance().get(Calendar.DATE));
					endTime.setTime(min);
					endTime.set(GregorianCalendar.getInstance().get(
							Calendar.YEAR), GregorianCalendar.getInstance()
							.get(Calendar.MONTH), GregorianCalendar
							.getInstance().get(Calendar.DATE));

					endTime.set(Calendar.HOUR_OF_DAY, 13);
					endTime.add(Calendar.MINUTE, 1);
					entities[2] = new StockEntity(
							StockEntity.RANGE_TYPE_DAY, startTime, endTime);
				} else {
					Date todayday = DateUtil.getCurrentDate();
					GregorianCalendar startTime = new GregorianCalendar();
					GregorianCalendar endTime = new GregorianCalendar();

					startTime.setTime(todayday);
					startTime.set(Calendar.HOUR_OF_DAY, 9);
					startTime.set(Calendar.MINUTE, 0);
					startTime.set(Calendar.SECOND, 0);
					endTime.setTime(todayday);
					endTime.set(Calendar.HOUR_OF_DAY, 13);
					endTime.set(Calendar.MINUTE, 30);
					endTime.set(Calendar.SECOND, 0);
					entities[2] = new StockEntity(
							StockEntity.RANGE_TYPE_DAY, startTime, endTime);

					entities[2].volume = Integer.parseInt(token.nextToken());
				}

				/* 成交價 */
				token.nextToken();
				token.nextToken();
				entities[2].ohlc.close = Double.parseDouble(token
						.nextToken());
				token.nextToken();
				token.nextToken();
				/* 開盤價 最高 最低 */
				entities[2].ohlc.open = Double.parseDouble(token
						.nextToken());
				entities[2].ohlc.high = Double.parseDouble(token
						.nextToken());
				entities[2].ohlc.low = Double.parseDouble(token.nextToken());

				wtx2Found = true;
			}
		}		
		
		return entities;
		
	}

	public static void main(String[] args) {
		StockEntity[] entities = new CNYesGather().getInstantWTXPrice();
		System.out.println(entities[0]);
		System.out.println(entities[1]);
		System.out.println(entities[2]);
	}
}
